package com.gaogzhen.designPattern.guardedSuspension;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 简单的下载器
 */
public class DownLoader {
    /**
     * 下载url地址对应的网页内容
     * @param url       url地址
     * @return   List<String>       url地址对应的网页内容
     * @throws IOException          IO异常
     */
    public static List<String> download(String url) throws IOException {
        // http链接
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        List<String> lines = new ArrayList<>();
        // 流读取网页内容
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 内容存如list
                lines.add(line);
            }
        }
        return lines;
    }
}
